package com.ch.home.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

/**
 * Created by CHEN on 2019/8/14.
 */
@RestController
@CrossOrigin
public class TestController {


    @GetMapping("/stream")
    public void getStreamData(HttpServletResponse response) {
        File file=new File("C:\\Users\\YBJB\\Pictures\\Saved Pictures\\安迷修与神禁耀.png");
        ServletOutputStream out=null;
        try {
            FileInputStream instream=new FileInputStream(file);
            byte[] b=new byte[16384];
            int length=0;
            BufferedInputStream buf=new BufferedInputStream(instream);
            out=response.getOutputStream();
            BufferedOutputStream bot=new BufferedOutputStream(out);
            int i=0;
            while((length=buf.read(b))!=-1) {
                bot.write(b,0, b.length);
                System.out.println("--------------------------"+i++ );
            }
            System.out.println("ok");

        } catch (Exception  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @GetMapping("/vedio")
    public void getVedio(HttpServletResponse response) {
        File file=new File("D:\\视频存放处\\BilibiliVedios\\50898828\\1\\50898828_1_0.flv");
        ServletOutputStream out=null;
        try {
            System.out.println(file );
            FileInputStream instream=new FileInputStream(file);
            byte[] b=new byte[16384];
            int length=0;
            BufferedInputStream buf=new BufferedInputStream(instream);
            out=response.getOutputStream();
            BufferedOutputStream bot=new BufferedOutputStream(out);

            int i=0;

            while((length=buf.read(b))!=-1) {
                bot.write(b,0, b.length);
                System.out.println("--------------------------"+i++ );
            }
        } catch (Exception  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> uploadImageFile(@RequestParam(value = "image" ,required = false) MultipartFile uploadImage,
                                                      @RequestParam(value = "type",required = false) String type){
        JSONObject json = new JSONObject();
        try {
            if(uploadImage==null){
                json.put("STATUS", "ERROR");
                json.put("MSG", "上传失败，上传图片数据为空");
                return new ResponseEntity<JSONObject>(json, HttpStatus.BAD_REQUEST);
            }

            String suffix = uploadImage.getContentType().toLowerCase();//图片后缀，用以识别哪种格式数据
            suffix = suffix.substring(suffix.lastIndexOf("/")+1);

            if(suffix.equals("jpg") || suffix.equals("jpeg") || suffix.equals("png") || suffix.equals("gif")) {
                String fileName = type + "_" + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
                String imgFilePath = "C:\\Users\\YBJB\\Desktop\\乡味\\";//新生成的图片

                File targetFile = new File(imgFilePath, fileName);
                if(!targetFile.getParentFile().exists()){ //注意，判断父级路径是否存在
                    targetFile.getParentFile().mkdirs();
                }
                //保存
                uploadImage.transferTo(targetFile);

                json.put("STATUS", "200");
                json.put("MSG", "上传图片成功");
                return new ResponseEntity<JSONObject>(json, HttpStatus.OK);
            }
//            log.error("系统异常，上传图片格式非法");
            json.put("STATUS", "ERROR");
            json.put("MSG", "上传图片格式非法");
            return new ResponseEntity<JSONObject>(json, HttpStatus.BAD_REQUEST);//500,系统异常
        } catch (Exception e) {
//            log.error("系统异常，上传图片失败,msg={}",e.getMessage());

            json.put("STATUS", "ERROR");
            json.put("MSG", "系统异常，上传图片失败");
            return new ResponseEntity<JSONObject>(json, HttpStatus.INTERNAL_SERVER_ERROR);//500,系统异常
        }
    }

    @RequestMapping(value = "/uploadVedio", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> uploadVedioFile(@RequestParam(value = "vedio" ,required = false) MultipartFile uploadVedio,
                                                      @RequestParam(value = "username",required = false) String type){
        JSONObject json = new JSONObject();
        try {
            if(uploadVedio==null){
                json.put("STATUS", "ERROR");
                json.put("MSG", "上传失败，上传图片数据为空");
                return new ResponseEntity<JSONObject>(json, HttpStatus.BAD_REQUEST);
            }

            String suffix = uploadVedio.getContentType().toLowerCase();//图片后缀，用以识别哪种格式数据
            suffix = suffix.substring(suffix.lastIndexOf("/")+1);

            if(suffix.equals("mp4") || suffix.equals("jpeg") || suffix.equals("png") || suffix.equals("gif")) {
                String fileName = type + "_" + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
                String imgFilePath = "C:\\Users\\YBJB\\Desktop\\乡味\\";//新生成的图片

                File targetFile = new File(imgFilePath, fileName);
                if(!targetFile.getParentFile().exists()){ //注意，判断父级路径是否存在
                    targetFile.getParentFile().mkdirs();
                }
                //保存
                uploadVedio.transferTo(targetFile);

                json.put("STATUS", "200");
                json.put("MSG", "上传图片成功");
                return new ResponseEntity<JSONObject>(json, HttpStatus.OK);
            }
//            log.error("系统异常，上传图片格式非法");
            json.put("STATUS", "ERROR");
            json.put("MSG", "上传图片格式非法");
            return new ResponseEntity<JSONObject>(json, HttpStatus.BAD_REQUEST);//500,系统异常
        } catch (Exception e) {
//            log.error("系统异常，上传图片失败,msg={}",e.getMessage());

            json.put("STATUS", "ERROR");
            json.put("MSG", "系统异常，上传图片失败");
            return new ResponseEntity<JSONObject>(json, HttpStatus.INTERNAL_SERVER_ERROR);//500,系统异常
        }
    }

}
